﻿using System;
using System.Data.OleDb;
using System.Data;

namespace shuju_jiange.Com
{
    class Database
    {
        private static String db_name = "";
        private static String constr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=dbname";
        private static OleDbConnection con = null;
        private static OleDbDataAdapter adapter = null;
        private static OleDbCommand cmd = null;

        public static OleDbConnection getcon()
        {
            return con;
        }

        public static void init_db(String p_db)
        {
            db_name = p_db;
        }

        public static bool db_open()
        {
            constr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + db_name;
            bool result = true;
            close_db();
            try
            {
                con = new OleDbConnection();
                con.ConnectionString = constr;
                con.Open();
                cmd = new OleDbCommand();
                cmd.Connection = con;
                //run_query("use collect_report");
            }
            catch (Exception)
            {
                close_db();
                result = false;
            }
            return result;
        }

        public static bool close_db()
        {
            bool result = true;
            try
            {
                if (cmd != null)
                {
                    cmd.Dispose();
                    cmd = null;
                }
                if (con != null)
                {
                    if (con.State != ConnectionState.Closed)
                        con.Close();
                    con.Dispose();
                    con = null;

                }
            }
            catch (Exception)
            {
                result = false;
            }
            return result;
        }

        public static bool is_active()
        {
            bool result = false;
            if ((con != null) && (cmd != null))
            {
                if (con.State == ConnectionState.Open)
                {
                    result = true;
                }
            }
            return result;
        }

        public static bool check_reinitdb()
        {
            bool result = true;
            if (!is_active())
            {
               result = db_open();
            }
            return result;
        }


        public static DataTable run_query(String sql_str)
        {
            DataTable dtab = new DataTable();
            if (check_reinitdb())
            {
                try
                {
                    adapter = new OleDbDataAdapter(sql_str, con);
                    adapter.Fill(dtab);
                    adapter.Dispose();
                }
                catch (Exception)
                {
                    if (adapter != null)
                        adapter.Dispose();
                    dtab = null;
                }
            }
            return dtab;
        }

        public static int run_excute(String sql_str)
        {
            int ret = 0;
            if (check_reinitdb())
            {
                try
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = sql_str;
                    ret = cmd.ExecuteNonQuery();
                }
                catch (Exception)
                {
                    ret = -1;
                }
            }
            return ret;
        }


        public static int get_count(String sql_str)
        {
            int ret = 0;
            DataTable dtab = run_query(sql_str);
            if (dtab != null)
            {
                ret = dtab.Rows.Count;
                dtab.Dispose();
            }
            return ret;
        }


        public static String get_cols_str(String sql_str, String cols_name,String type)
        {
            String ret_str = "";
            ret_str = type == "num" ? "0" : "";
            DataTable dtab = Database.run_query(sql_str);
            if (dtab != null)
            {
                if (dtab.Rows.Count > 0)
                {
                    ret_str = dtab.Rows[0][cols_name].ToString();
                }
                dtab.Dispose();
            }
            return ret_str;
        }
    }
}
